/**
 * 字典模块数据操作文件
 */

const { Op } = require('sequelize');
const { sequelize } = require('./common');
const { dictionary } = sequelize.models;

/**
 * 添加字典数据
 * @param {*} model 要添加的数据
 * @returns 
 */
const add = model => dictionary.create(model);

/**
 * 修改字典数据
 * @param {*} model 
 * @returns 
 */
const update = model => dictionary.update(model, { where: { id: model.id } });

/**
 * 获取所有字典数据
 * @returns 
 */
const getAll = () => dictionary.findAll();

/**
 * 是否存在相同数据
 * @param {*} name 
 * @param {*} type 
 * @returns 
 */
const exists = (name, type, id = 0) => dictionary.findOne({
    where: {
        name,
        type,
        [Op.not]: {
            id
        }
    }
});

module.exports = {
    add,
    update,
    getAll,
    exists
}